//
//  MyFMDB.m
//  Chinese Dictionary
//
//  Created by ibokan on 13-7-30.
//  Copyright (c) 2013年 ibokan. All rights reserved.
//

#import "MyFMDB.h"

@implementation MyFMDB


#pragma mark 创建FMDB单例
//加线程锁
+(FMDatabase*)openDB
{
    NSString *docPath=[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    NSString *sqlPath=[docPath stringByAppendingPathComponent:@"mydatabase.sqlite"];
    
    NSString *originPath=[[NSBundle mainBundle] pathForResource:@"mydatabase" ofType:@"sqlite"];
    NSFileManager *fm=[NSFileManager defaultManager];
    if ([fm fileExistsAtPath:sqlPath]==NO) {
        NSError *err = nil;
        if([fm copyItemAtPath:originPath toPath:sqlPath error:&err] == NO)//如果拷贝失败
        {
            NSLog(@"open database error %@",[err localizedDescription]);//拷贝失败打印相关错误信息
            return nil;
        }
    }
    @synchronized(self){
    static FMDatabase * db=nil;
    if (!db) {
        db=[[FMDatabase alloc]initWithPath:sqlPath];
            if (![db open]) {
                MyNSLog("数据库打开失败！");
            }
            else
        return db;
        }
    else
    {
        if (![db open]) {
            MyNSLog("数据库打开失败！");
        }
        else;
        
        return db;
    }
    }
    return nil;
    
}
@end
